package com.ehouse.fragments;


import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.CursorAdapter;

import com.ehouse.R;
import com.ehouse.adapters.IrrigationItemAdapter;
import com.ehouse.database.IrrigationTable;

public class IrrigationsFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor>{


	// This is the Adapter being used to display the list's data.
	IrrigationItemAdapter mAdapter;

    // If non-null, this is the current filter the user has provided.
    String mCurFilter;

    @Override public void onActivityCreated(Bundle savedInstanceState) 
    {
        super.onActivityCreated(savedInstanceState);

        // Give some text to display if there is no data.  In a real
        // application this would come from a resource.
        setEmptyText(getResources().getString(R.string.no_irri));
        // Start out with a progress indicator.
        setListShown(false);

        getLoaderManager().initLoader(0, null, this);
    }

    public void reload(){
    	setListShown(false);
    	getLoaderManager().restartLoader(0, null, this);

		
    }
    
    static final String[] PROJECTION = new String[] { IrrigationTable.COLUMN_ID, IrrigationTable.COLUMN_STATE };
	public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
		return new CursorLoader(getActivity(), IrrigationTable.CONTENT_URI,
                PROJECTION, null, null,null);
	}

	public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
		
       mAdapter = new IrrigationItemAdapter(getActivity(), R.layout.irri_item, data, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

       setListAdapter(mAdapter);
       mAdapter.swapCursor(data);
       setListShown(true);
	}

	public void onLoaderReset(Loader<Cursor> arg0) {
		 mAdapter.swapCursor(null);
	}

}
